<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Forum Threads" %>
<script runat="server">
#Region "Diclaimer"
'Copyright (c) 2006, Mark Bracewell (bracewell at wgg dot com)
'All rights reserved.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met: 
'*

'* Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in the
'documentation and/or other materials provided with the distribution.
'* The name Mark Bracewell may not be used to endorse or promote products
'derived from this software without specific prior written permission. 
'* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#End Region
    Private IAdmin As Boolean = False
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then

            Dim topicid As Integer
            topicid = CInt(IIf(String.IsNullOrEmpty(Request("to")), 0, Request("to")))
            If topicid = 0 Then Response.Redirect("topics.aspx", True)

            Dim ti As ClubStarterKit.Data.ForumTopicInfo = ClubStarterKit.Web.Forum.GetTopicInfo(topicid)

            If Not User.IsInRole("Administrators") Then
                If (ti.section Or ti.visible = False) Then
                    Response.Redirect("topics.aspx", True)
                End If
                If ti.locked Then
                    NewThreadLink.Visible = False
                    ntimg.Visible = False
                End If
            End If
            NewThreadLink.NavigateUrl = "addpost.aspx?to=" & topicid.ToString() & "&pa=0"
            NewThreadLink.ToolTip = "Add New Thread to " & ti.topic & " ..."
            If User.Identity.IsAuthenticated = False Then
                NewThreadLink.Visible = False
                ntimg.Visible = False
            Else
                NewThreadLink.Visible = True
                ntimg.Visible = True
            End If
            TopicName.Text = ti.topic
            Desc.Text = ti.description
            
            IAdmin = User.IsInRole("Administrators")
            If IAdmin = True Then
                AdminGRID.Visible = True
            Else
                AdminGRID.Visible = False
                
            End If
        End If
    End Sub
    
    Private Function IsAdmin() As Boolean
        Return IAdmin
    End Function

    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            AddGlyph(GridView1, e.Row)
        End If
    End Sub
    
    Protected Sub AddGlyph(ByVal grid As GridView, ByVal item As GridViewRow)
        Dim glyph As Image = New Image()
        glyph.EnableTheming = False
        glyph.ImageAlign = ImageAlign.Bottom
        glyph.ImageUrl = String.Format("~/App_Themes/" & Page.Theme.ToString() & "/images/move{0}.gif", CType(IIf(grid.SortDirection = SortDirection.Ascending, "up", "down"), String))

        Dim i As Integer
        Dim colExpr As String
        For i = 0 To grid.Columns.Count - 1
            colExpr = grid.Columns(i).SortExpression
            If colExpr <> "" AndAlso colExpr = grid.SortExpression Then
                item.Cells(i).Controls.Add(glyph)
            End If
        Next
    End Sub

    Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        StickyGridView.Visible = (GridView1.PageIndex = 0)
    End Sub

    Protected Sub GridView1_Sorted(ByVal sender As Object, ByVal e As System.EventArgs)
        StickyGridView.Visible = (GridView1.PageIndex = 0)
    End Sub
    
    Protected Function IsRecentPost(ByVal lastpostdate As Object) As Boolean
        Return ClubStarterKit.Web.Forum.IsRecentPost(lastpostdate)
    End Function
    
    Protected Function RecentPostAge(ByVal lastpostdate As Object) As String
        Return ClubStarterKit.Web.Forum.RecentPostAge(lastpostdate)
    End Function
    
    Protected Function IsUnreadThread(ByVal lastpostdate As Object, ByVal threadid As Object) As Boolean
        If User.Identity.IsAuthenticated = False Then Return False
        If lastpostdate Is Nothing Or IsDBNull(lastpostdate) Then Return False
        Return ClubStarterKit.Web.Forum.IsThreadUnread(CInt(threadid), New Guid(Membership.GetUser.ProviderUserKey.ToString()), CDate(lastpostdate))
    End Function
    
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <div class="fullwidth">
            <table cellspacing="0" cellpadding="0" width="100%">
                <tr>
                    <td>
                        <asp:HyperLink ID="ForumsCrumb" NavigateUrl="Topics.aspx" runat="server">Forums</asp:HyperLink>
                        </td>
                    <td align="right">
                        <asp:Image ID="ntimg" runat="server" SkinID="Newthread" /> <asp:HyperLink ID="NewThreadLink" runat="server">New Thread</asp:HyperLink>
                        </td>
                </tr>
            </table>
        </div>
        <div class="fullwidth">
            <h3>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
                &nbsp;</h3>
            <h3>
                <asp:Label ID="TopicName" runat="server"></asp:Label>&nbsp;</h3>
            <div class="topicdesc">
                <asp:Label ID="Desc" runat="server"></asp:Label></div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                SelectCommand="GetThreads" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:QueryStringParameter Name="topicid" QueryStringField="to" Type="Int32" />
                    <asp:Parameter Name="sticky" DefaultValue="False" Type="boolean" />
                    <asp:Parameter Name="admin" DefaultValue="False" Type="boolean" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                SelectCommand="GetThreads" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:QueryStringParameter Name="topicid" QueryStringField="to" Type="Int32" />
                    <asp:Parameter Name="sticky" DefaultValue="True" Type="boolean" />
                    <asp:Parameter Name="admin" DefaultValue="False" Type="boolean" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
                SelectCommand="GetThreads" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:QueryStringParameter Name="topicid" QueryStringField="to" Type="Int32" />
                    <asp:Parameter Name="sticky" DefaultValue="False" Type="boolean" />
                    <asp:Parameter Name="admin" DefaultValue="True" Type="boolean" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:UpdatePanel ID="UpdatePanel12" runat="server">
            <ContentTemplate>
            <asp:GridView ID="AdminGRID" runat="server" AllowPaging="false" DataSourceID="SqlDataSource3"
                AllowSorting="false" AutoGenerateColumns="False" DataKeyNames="id" Width="100%"
                ShowHeader="True" Caption="Administrator Forum Threads" CaptionAlign="top"
                CssClass="stickygrid" GridLines="None">
                <Columns>
                    <asp:ImageField DataImageUrlField="locked" DataImageUrlFormatString="~/Images/stickythreadlocked{0}.gif"
                        AlternateText="Sticky Thread">
                        <ItemStyle Width="24px" />
                    </asp:ImageField>
                    <asp:TemplateField HeaderText="Subject" SortExpression="subject">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("threadid", "Thread.aspx?th={0}") %>'
                                Text='<%# Eval("subject") %>' CssClass="threadlistlink"></asp:HyperLink>
                            <asp:Image AlternateText="Un-Read Message Indicator" ToolTip="Un-Read Messages" ID="Image3"
                                SkinID="unread" runat="server" Visible='<%# IsUnreadThread(Eval("lastpostdate"), Eval("threadid")) %>' />
                            <br />
                            by
                            <asp:Label ID="StarterName" runat="server" Text='<%# Eval("startername") %>'></asp:Label>
                            on
                            <asp:Label ID="SatrtDate" runat="server" Text='<%# Eval("postdate") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle CssClass="threadlistitem" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="replies" HeaderText="Replies" SortExpression="replies">
                        <ItemStyle Width="16px" HorizontalAlign="Right" />
                        <HeaderStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="views" HeaderText="Views" SortExpression="views">
                        <ItemStyle Width="16px" HorizontalAlign="Right" />
                        <HeaderStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Last Post" SortExpression="lastpostdate">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("lastpostdate") %>'></asp:Label>
                            <br />
                            by
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("displayname") %>'></asp:Label>
                            <asp:Image AlternateText="Recent Message Indicator" ToolTip='<%# RecentPostAge(Eval("lastpostdate")) %>' ID="Image2"
                                SkinID="recent" runat="server" Visible='<%# IsRecentPost(Eval("lastpostdate")) %>' />
                        </ItemTemplate>
                        <ItemStyle Width="130px" Wrap="False" CssClass="threadlistitem" />
                    </asp:TemplateField>
                </Columns>
                <PagerSettings Mode="NumericFirstLast" />
            </asp:GridView>
            </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdateProgress id="UpdateProgress32" runat="server" AssociatedUpdatePanelID="UpdatePanel12">
                <progresstemplate>
                    <asp:Image ID="loading12" runat="server" SkinID="loading" />
                </progresstemplate>
            </asp:UpdateProgress>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:GridView ID="StickyGridView" runat="server" AllowPaging="false" DataSourceID="SqlDataSource2"
                AllowSorting="false" AutoGenerateColumns="False" DataKeyNames="id" Width="100%"
                ShowHeader="True" Caption="Announcements, FAQs and Sticky Threads" CaptionAlign="top"
                CssClass="stickygrid" GridLines="None">
                <Columns>
                    <asp:ImageField DataImageUrlField="locked" DataImageUrlFormatString="~/Images/stickythreadlocked{0}.gif"
                        AlternateText="Sticky Thread">
                        <ItemStyle Width="24px" />
                    </asp:ImageField>
                    <asp:TemplateField HeaderText="Subject" SortExpression="subject">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("threadid", "Thread.aspx?th={0}") %>'
                                Text='<%# Eval("subject") %>' CssClass="threadlistlink"></asp:HyperLink>
                            <asp:Image AlternateText="Un-Read Message Indicator" ToolTip="Un-Read Messages" ID="Image3"
                                SkinID="unread" runat="server" Visible='<%# IsUnreadThread(Eval("lastpostdate"), Eval("threadid")) %>' />
                            <br />
                            by
                            <asp:Label ID="StarterName" runat="server" Text='<%# Eval("startername") %>'></asp:Label>
                            on
                            <asp:Label ID="SatrtDate" runat="server" Text='<%# Eval("postdate") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle CssClass="threadlistitem" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="replies" HeaderText="Replies" SortExpression="replies">
                        <ItemStyle Width="16px" HorizontalAlign="Right" />
                        <HeaderStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="views" HeaderText="Views" SortExpression="views">
                        <ItemStyle Width="16px" HorizontalAlign="Right" />
                        <HeaderStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Last Post" SortExpression="lastpostdate">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("lastpostdate") %>'></asp:Label>
                            <br />
                            by
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("displayname") %>'></asp:Label>
                            <asp:Image AlternateText="Recent Message Indicator" ToolTip='<%# RecentPostAge(Eval("lastpostdate")) %>' ID="Image2"
                                SkinID="recent" runat="server" Visible='<%# IsRecentPost(Eval("lastpostdate")) %>' />
                        </ItemTemplate>
                        <ItemStyle Width="130px" Wrap="False" CssClass="threadlistitem" />
                    </asp:TemplateField>
                </Columns>
                <PagerSettings Mode="NumericFirstLast" />
            </asp:GridView>
            </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdateProgress id="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
                <progresstemplate>
                    <asp:Image ID="loading1" runat="server" SkinID="loading" />
                </progresstemplate>
            </asp:UpdateProgress>
            <p>
            </p>
            <asp:UpdatePanel ID="UpdatePanel22" runat="server">
            <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1"
                AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" Width="100%"
                OnRowCreated="GridView1_RowCreated" PagerSettings-Mode="NumericFirstLast" OnPageIndexChanged="GridView1_PageIndexChanged"
                PagerSettings-Position="TopAndBottom" PagerStyle-HorizontalAlign="Right" PagerStyle-Font-Bold="true"
                PagerSettings-PageButtonCount="5" OnSorted="GridView1_Sorted" CssClass="normalgrid" GridLines="None">
                <Columns>
                    <asp:ImageField DataImageUrlField="locked" DataImageUrlFormatString="~/Images/threadlocked{0}.gif"
                        AlternateText="Thread">
                        <ItemStyle Width="24px" />
                    </asp:ImageField>
                    <asp:TemplateField HeaderText="Subject" SortExpression="subject">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("threadid", "Thread.aspx?th={0}") %>'
                                Text='<%# Eval("subject") %>' CssClass="threadlistlink"></asp:HyperLink>
                            <asp:Image AlternateText="Un-Read Message Indicator" ToolTip="Un-Read Messages" ID="Image3"
                                SkinID="unread" runat="server" Visible='<%# IsUnreadThread(Eval("lastpostdate"), Eval("threadid")) %>' />
                            <br />
                            by
                            <asp:Label ID="StarterName" runat="server" Text='<%# Eval("startername") %>'></asp:Label>
                            on
                            <asp:Label ID="SatrtDate" runat="server" Text='<%# Eval("postdate") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle CssClass="threadlistitem" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="replies" HeaderText="Replies" SortExpression="replies">
                        <ItemStyle Width="16px" HorizontalAlign="Right" />
                        <HeaderStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="views" HeaderText="Views" SortExpression="views">
                        <ItemStyle Width="16px" HorizontalAlign="Right" />
                        <HeaderStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Last Post" SortExpression="lastpostdate">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("lastpostdate") %>'></asp:Label>
                            <br />
                            by
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("displayname") %>'></asp:Label>
                            <asp:Image AlternateText="Recent Message Indicator" ToolTip='<%# RecentPostAge(Eval("lastpostdate")) %>' ID="Image2"
                                SkinID="recent" runat="server" Visible='<%# IsRecentPost(Eval("lastpostdate")) %>' />
                        </ItemTemplate>
                        <ItemStyle Width="130px" Wrap="False" CssClass="threadlistitem" />
                    </asp:TemplateField>
                </Columns>
                <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" Position="TopAndBottom" />
                <PagerStyle Font-Bold="True" HorizontalAlign="Right" />
            </asp:GridView>
            </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdateProgress id="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel22">
                <progresstemplate>
                    <asp:Image ID="loading2" runat="server" SkinID="loading" />
                </progresstemplate>
            </asp:UpdateProgress>
        </div>
    </div>
</asp:Content>
